package com.ajz.common.utils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import com.baidu.ueditor.define.FileType;
import com.ajz.common.constants.OSSClientConstants;

/**
 * 判断文件类别工具类
 * 
 * @author xyj
 *
 */
public class FileTypeUtils {
	/**
	 * 判断文件是否是视频类型
	 * 
	 * @param 文件后缀
	 * @return
	 */
	public static boolean isVideo(String suffix) {
		String[] type = { ".mp4", ".wav", ".avi", ".rm", ".rmvb", ".wmv", ".mpg", ".mpeg", ".mpe", ".mov", ".3gp",
				".dat", ".mkv", ".flv", ".vob", ".swf" };
		for (String str : type) {
			if (suffix.equals(str)) {
				return true;
			}
		}
		return false;

	}

	/**
	 * 判断是否是图片类型
	 * 
	 * @param suffix
	 * @return
	 */
	public static boolean isImage(String suffix) {
		String[] type = { ".jpg", ".png", ".jpeg", ".gif", ".bmp", ".ico", ".tmp" };
		for (String str : type) {
			if (suffix.equals(str)) {

				return true;
			}
		}
		return false;

	}

	/**
	 * 判断是否是文档类型
	 * 
	 * @param suffix
	 * @return
	 */
	public static boolean isDoc(String suffix) {
		String[] type = { ".doc", ".docx", ".txt", ".pem", ".xls", ".xlsx", ".ppt", ".pptx" };
		for (String str : type) {
			if (suffix.equals(str)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断是否是音频类型
	 * 
	 * @param suffix
	 * @return
	 */
	public static boolean isAudio(String suffix) {
		String[] type = { ".MP3", ".AAC", ".WAV", ".WMA", ".CDA", ".FLAC", ".M4A", ".MID", ".MKA", ".MP2", ".MPA",
				".MPC", ".APE", ".OFR", ".OGG", ".RA", ".WV", ".TTA", ".AC3", ".DTS" };
		for (String str : type) {
			if (suffix.equals(str)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断是否是音频类型
	 * 
	 * @param suffix
	 * @return
	 */
	public static boolean isApk(String suffix) {
		String[] type = { ".apk" };
		for (String str : type) {
			if (suffix.equals(str)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 生成文件在oss上的目标路径
	 * 
	 * @param FileType
	 * @return
	 */
	public static String OSSFileTargetPath(String FileType) {
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("YYYY-MM-dd");
		String targetPath = OSSClientConstants.FOLDER + FileType + sf.format(date) + "/";
		return targetPath;
	}

	/**
	 * 获取准备转存的新文件名加后缀
	 * 
	 * @param Multfile
	 * @return
	 */
	public static String getFileName(MultipartFile Multfile) {
		// 原文件名加后缀
		String originFileName = Multfile.getOriginalFilename();
		// 获取原文件后缀
		String suffix = FileType.getSuffixByFilename(originFileName);
		// 生成新的文件名
		String fileName = System.currentTimeMillis() + Sequence.nextId();

		return fileName + suffix;
	}

	/**
	 * MultipartFile转存为新名称的File
	 * 
	 * @param Multfile
	 * @param filePath
	 * @return
	 */
	public static File MultipartFileToNewFile(MultipartFile Multfile, String filePath) {
		File file = null;
		try {
			String path = filePath + getFileName(Multfile);
			file = new File(path);
			// MultipartFile 转存为新的临时File文件
			FileUtils.copyInputStreamToFile(Multfile.getInputStream(), file);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return file;
	}
}
